Prevent duplicate DLQ messages when afterDlq throws after successful publish#76
Open
Dimitris-Ilias wants to merge 1 commit intoWorkable:masterfrom
Open
Conversation
ce06bda to
7c3e875
Compare
There was a problem hiding this comment.
Pull request overview
Fixes a duplication bug in BaseQueueHandler.addToDLQ where a thrown afterDlq hook could cause the catch-path fallback publish to send a second (duplicate) DLQ message even after the initial DLQ publish succeeded.
Changes:
- Track whether the primary DLQ publish succeeded (
dlqPublished) and skip fallback republish when it did. - Update/extend tests to assert the “no duplicate DLQ entry” invariant and validate raw-buffer fallback behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| ts/base-queue-handler.ts | Adds a dlqPublished flag to prevent fallback republish when the first DLQ publish already succeeded. |
| test/base-queue-handler.test.ts | Reworks DLQ tests to assert single DLQ delivery and raw-buffer fallback on publish failure. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
9ac8f59 to
ee526ab
Compare
ee526ab to
2f466b4
Compare
Dimitris-Ilias
commented
May 8, 2026
| }); | ||
|
|
||
| it('should add raw buffer to dlq when afterDlq throws to prevent double-encoding', async function () { | ||
| it('should not duplicate dlq message when afterDlq throws after successful publish', async function () { |
Contributor
Author
There was a problem hiding this comment.
ℹ️ This is the new test but diff is a little out of whack because these two tests have very similar setup
aeafbfd to
2f466b4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
As found on #75, on
BaseQueueHandler.addToDLQwhen anafterDlqhook throws after a successful DLQ publish a duplicate message is published on the DLQ.This PR tackles this issue
Before this PR
BaseQueueHandler.addToDLQwraps the decode → publish →afterDlqsequence in a single try/catch. The catch-path republishes the message as a raw-buffer fallback so that messages still reach the DLQ when something goes wrong.decode(msg)throws or when the firstrabbit.publishis rejected by the broker — the DLQ entry is missing and the catch-path provides it.afterDlqthrows after a successful first publish, the catch-path runs anyway and publishes a second copy of the same message. The DLQ ends up with two entries for one input.afterDlqfailure under load.After this PR
addToDLQtracks whether the first publish resolved by setting a localdlqPublishedflag. The catch-path republishes only when!dlqPublished, so a successful publish + failingafterDlqproduces exactly one DLQ entry.decodethrows → fallback republish runs → 1 entry.rabbit.publishrejects → fallback republish runs → 1 entry.afterDlqthrows after success → fallback skipped → 1 entry (new behavior).